package com.mysharding.test001;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class test1 {
    public static int day[] = {1, 2, 3, 4, 5};//一个星期5天
    public static int grade[] = {1};//6个年级2, 3, 4, 5, 6
    public static int node[] = {1, 2, 3, 4, 5, 6, 7};//一天6节课
    public static int teacher[] = {1, 2, 3};//1语文老师，2数学老师，3体育老师
    public static List<String> stringList = new ArrayList<>();
    public static List<String> stringList2 = new ArrayList<>();

    //天-课节-老师-年级
    //1    1    1   1
    //1    1    1   2
    //1    2    1   1
    //1    1    2   1
    public static void init() {
        for (int i1 : day) {
            for (int i2 : node) {
                for (int i3 : teacher) {
                    for (int i4 : grade) {
                        stringList2.add(String.valueOf(i1) + String.valueOf(i2) + String.valueOf(i3) + String.valueOf(i4));
                    }
                }
            }
        }

    }

    public static boolean getFilter2(String str, int size) {
        List<String> stringList1 = stringList.stream().filter(s -> {
            if (s.substring(0, 1).equals(str.substring(0, 1)) && s.substring(3, 4).equals(str.substring(3, 4))) {
                if (s.substring(0, 1).equals("1") || s.substring(0, 1).equals("2") || s.substring(0, 1).equals("3")) {
                    return true;
                }
            }
            return false;
        }).collect(Collectors.toList());
        if (stringList1.size() >= size) {
            return false;
        }
        return true;
    }

    public static boolean getFilter3(String str, int size, String teacher) {
        if (!teacher.equals(str.substring(2, 3))) {
            return true;
        }
        List<String> stringList1 = stringList.stream().filter(s -> {
            if (s.substring(0, 1).equals(str.substring(0, 1)) && s.substring(3, 4).equals(str.substring(3, 4))) {
                if (teacher.equals(s.substring(2, 3)) || teacher == s.substring(2, 3)) {
                    return true;
                }
            }
            return false;
        }).collect(Collectors.toList());
        if (stringList1.size() >= size) {
            return false;
        }
        return true;
    }

    public static boolean getFilter4(String str, int size) {
        List<String> stringList1 = stringList.stream().filter(s -> {
            if (s.substring(0, 1).equals(str.substring(0, 1)) && s.substring(3, 4).equals(str.substring(3, 4))) {
                if (s.substring(0, 1).equals("4") || s.substring(0, 1).equals("5") || s.substring(0, 1).equals("6")) {
                    return true;
                }
            }
            return false;
        }).collect(Collectors.toList());
        if (stringList1.size() >= size) {
            return false;
        }
        return true;
    }

    public static boolean getFilter1(String str) {
        List<String> stringList1 = stringList.stream().filter(s -> {
            if (s.substring(0, 1).equals(str.substring(0, 1)) && s.substring(2, 4).equals(str.substring(2, 4))) {
                return true;
            }
            return false;
        }).collect(Collectors.toList());
        if (stringList1.size() >= 3) {
            return false;
        }
        return true;
    }


    public static boolean getConstraint(String str) {
        if (!getFilter3(str, 1, "3")) {
            return false;
        }
        if (!getFilter1(str)) {
            return false;
        }


//        if (!getFilter2(str, 3)) {
//            return false;
//        }
//        if (!getFilter3(str, 3)) {
//            return false;
//        }
        List<String> now = stringList.stream().filter(s -> {
            if (s.substring(0, 2).equals(str.substring(0, 2))) {
                if (s.substring(2, 3).equals(str.substring(2, 3)) || s.substring(3, 4).equals(str.substring(3, 4))) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }).collect(Collectors.toList());
        return now.size() > 0 ? false : true;
    }

    public static String getRandom() {
        Random random = new Random();
        int n = random.nextInt(stringList2.size());
        String str = stringList2.get(n);
        stringList2.remove(n);
        return str;
    }

    public static void main(String[] args) {
        init();
        stringList.add(getRandom());
        while (stringList2.size() > 0) {
            String s = getRandom();
            if (getConstraint(s)) {
                stringList.add(s);
            }
            ;
        }
        List<String> list = stringList.stream().sorted().collect(Collectors.toList());
//        System.out.println(JSON.toJSONString(list));
        List list2 = list.stream().filter(s -> {
            if (s.substring(0, 1).equals("1")) {
                if (s.substring(3, 4).equals("1")) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(list2));

    }
}
